package com.exception.exceptiondemo.service.impl;


import com.exception.exceptiondemo.exception.CustomException;
import com.exception.exceptiondemo.exceptionEnum.CustomExceptionType;
import com.exception.exceptiondemo.response.AjaxResponse;
import com.exception.exceptiondemo.service.CustomService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CustomServiceImpl implements CustomService {

    /**
     * 模拟用户输入数据导致的校验异常
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getCustom(Long id) {
        if (id <= 0) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "您输入的数据不符合业务逻辑，请确认后重新输入！");
        } else {
            Map<String, Object> m = new HashMap<String, Object>(3);
            m.put("customId", id);
            m.put("customName", "李四");
            m.put("customAge", "女");
            return m;
        }

    }

    /**
     * 模拟系统异常
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResponse putCustom(Long id) throws CustomException {
        try {
            Class.forName("com.mysql.jdbc.xxxx.Driver");
            return AjaxResponse.success();
        } catch (ClassNotFoundException e) {
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "业务逻辑出现异常：ClassNotFoundException！");
        }
    }

}
